﻿
import { addPortalMenu, updatePortalMenu} from "@/api/menu/portalMenu";
import {uploadPortalMenuFile} from "@/api/baseUtil";
/**
 * 重置
 * @param that
 */
export function resetPortalForm(that) {
  that.form = {
    id: null,
    parentId: null,
    parentName: null,
    menuName: null,
    subtitleName: null,
    menuCode: null,
    imgUrl: null,
    explain: null,
    menuLevel: 1,
    menuType: null,
    status: 0,
    color: null,
    orderNum: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  that.resetForm("form");
}

/**
 * 关闭弹框
 * @param that
 * @param openKey
 */
export function cancelPortalForm(that,openKey) {
  that[openKey] = false;
  resetPortalForm(that);
}
/**
 * 提交修改
 * @param that
 * @param form 数据
 * @param openKey 弹框
 */
export function submitPortalForm(that,form,openKey) {
  that.$refs["form"].validate(valid => {
    if (valid) {
      if (form.id != null) {
        updatePortalMenu(form).then(response => {
          if (response.code === '200') {
            that.msgSuccess("修改成功");
            that[openKey] = false;
            that.$emit('getList')
          }
        });
      } else {
        addPortalMenu(form).then(response => {
          if (response.code === '200') {
            that.msgSuccess("新增成功");
            that[openKey] = false;
            that.$emit('getList')
          }
        });
      }
    }
  });
}

/**
 * 提交图片
 * @param that
 * @param file
 */
export function submitPortalImg(that,file) {
  if (file.type.indexOf("image/") == -1) {
    that.msgError("文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。");
  } else {
    let formData = new FormData();
    formData.append("file", file);
    uploadPortalMenuFile(formData).then(response => {
      that.form.imgUrl = response.fileName
    });
  }
}
